<template>
  <el-form :model="form" :rules="rules" ref="formRef">
    <el-form-item label="快递单号" prop="shippingCode" :label-width="formLabelWidth">
      <el-input v-model.number="form.shippingCode" placeholder="请输入快递单号"></el-input>
    </el-form-item>
    <!-- <el-form-item label="快递公司" prop="package" :label-width="formLabelWidth">
      <el-input v-model="form.shippingName" placeholder="请输入快递公司"></el-input>
    </el-form-item>
    <el-form-item label="服务机构" prop="service" :label-width="formLabelWidth">
      <el-input v-model="form.service" placeholder="请输入服务机构"></el-input>
    </el-form-item>-->
    <el-form-item label="快递公司" prop="package" :label-width="formLabelWidth">
      <el-select v-model="form.shippingName" @change="packageChange" placeholder="请选择">
        <el-option
          v-for="(item,index) in packageList"
          :key="index"
          :label="item.logisticsName"
          :value="item.logisticsName"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="服务机构" prop="service" :label-width="formLabelWidth">
      <el-select v-model="form.service" placeholder="请选择">
        <el-option
          v-for="(item,index) in packageServiceList"
          :key="index"
          :label="item.servicerName"
          :value="item.servicerName"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item :label-width="formLabelWidth">
      <el-button plain @click="resetForm('formRef')">取消</el-button>
      <el-button type="primary" @click="submitForm('formRef')">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: "",
        shippingCode: "", //快递单号
        shippingName: "", //快递公司
        service: "" //服务机构
      },
      packageList: [],
      packageServiceList: [],
      formLabelWidth: "120px",
      rules: {
        shippingCode: [
          { required: true, message: "请输入订单编号", trigger: "blur" }
        ],
        // package: [
        //   { required: true, message: "请选择快递公司", trigger: "change" }
        // ],
        service: [
          { required: true, message: "请选择服务机构", trigger: "change" }
        ]
      }
    };
  },
  mounted() {
    this.getPackageList();
  },
  methods: {
    getPackageList() {
      this.$service.getPackageList().then(res => {
        //console.log(res);
        this.packageList = res.data || [];
      });
    },
    packageChange(e) {
      this.form.service = "";
      let param = { logisticsName: e };
      this.$service.getPackageService(param).then(res => {
        //console.log(res.data);
        this.packageServiceList = res.data || [];
      });
    },
    initData(id) {
      this.form.id = id;
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //console.log(this.form);
          var eng ="";
          this.packageList.forEach((item,index) =>{
            if(this.form.shippingName ==item.logisticsName){
              eng =item.eng
            }
          });
          var postdata = {
            id: this.form.id, //订单id
            shippingName: this.form.shippingName, //物流名称
            servicerName: this.form.service, //服务商名称
            shippingCode: this.form.shippingCode, //物流单号
            eng: eng //物流编码
          };
          this.$service.sendItem(postdata).then(res => {
            if (res.code == "00000") {
              this.$message({
                message: "发货成功!"
              });
              this.$refs[formName].resetFields();
              this.$emit("submit");
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$emit("close", false);
      this.$refs[formName].resetFields();
    }
  }
};
</script>

